var myDate = new Date();
var year = myDate.getFullYear();   //获取完整的年份(4位,1970-????)
var month =myDate.getMonth()+1;      //获取当前月份(0-11,0代表1月)
var day = myDate.getDate();       //获取当前日(1-31)
if(month<10)month="0"+month;
if(day<10)day="0"+day;
var tody = year+"-"+month+"-"+day;
var userinfo = null;

$(document).ready(function(){
	cw.common.getUserSession(getUserInfo);//查询用户
	$("#CHANGE_BEGIN_DATE").focus(function(){WdatePicker({dateFmt:'yyyy-MM-dd',oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})});
	$("#CHANGE_BEGIN_DATE").val(tody);
	//$("#CHANGE_END_DATE").focus(function(){WdatePicker({dateFmt:'yyyy-MM-dd',oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})});
	//$("#CHANGE_END_DATE").val(tody);
	$('#subImg').bind('click',subButton);
	validate();
})
var getUserInfo = function(us){
	userinfo=us;
	$("#createBy").html(userinfo.USER_NAME);
	$("#createDate").html(tody);
	queryPERSONNAL();
}
//查询人员
var queryPERSONNAL = function(){
	$.post(cw.common.path+'/json',
			{
				action : 'QUERY_T_PERSONNAL_ACTION'
			}, function(data){
				if (data.success) {
					var length = data.root.T_PERSONNAL.rs.length;
					if(length > 0){
						for(var i=0;i<length;i++) {
							var obj=data.root.T_PERSONNAL.rs[i];
							$("#PERSON").append("<option value='"+obj.PERSONNAL_NO+"'>"+obj.NAME+"</option>");
						}
					}
				} else {
					alert(data.errors.errmsg);
				}
			}, 'json');
}
function subFrm(){
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		$('#subImg').unbind('click');
	    		var vobj=data.root.T_CHECK_INFO.rs[0];
	    		alert("添加资产盘点单成功!");
	    		QUERY_T_CHECK_INFO_DETAILBY_PAGER_ACTION(vobj.ID);
	      	}else{
	      		alert(data.errors.errmsg);
	      		$('#subImg').bind('click',subButton);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}
function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){$('#subImg').bind('click',subButton);alert(msg);},onsuccess:function(){subFrm();return false;}});
	$("#CHECK_NO").formValidator({onshow:"请输入资产单点号",onfocus:"资产单点号由英文或数字中文组成"}).inputValidator({min:1,onerror:"请输入资产单点号"}).inputValidator({max:32,onerror:"请输入资产单点号过长，长度不超过32个字符"})
}


//资产详细信息
var QUERY_T_CHECK_INFO_DETAILBY_PAGER_ACTION = function(id){
	
	var url = cw.common.path + '/json?action=QUERY_T_CHECK_INFO_DETAILBY_PAGER_ACTION';
	url += "&CHECK_NO="+id;
	var xmlname = "T_CHECK_INFO_DETAIL";
	var pagePanel='#T_CHECK_INFO_DETAIL_pager';
	var result='#T_CHECK_INFO_DETAIL_LIST';
	var pageSize=10;
	var defaultHtml='<li><em style="text-align: center;width:100%">没有搜索到您要查询的数据...</em></li>';
	cw.common.changePage(url,pageSize,xmlname,result,fn_QUERY_T_CHECK_INFO_DETAILBY_PAGER_ACTION,pagePanel,defaultHtml);
}
var fn_QUERY_T_CHECK_INFO_DETAILBY_PAGER_ACTION = function(obj,result,i){
	var str='<li id="bar_'+obj.ID+'">';
	str+='<em style="width: 10%; text-align: center;">'+obj.BARCODE+'</em>';
	str+='<em style="width: 12%; text-align: center;">'+obj.FIXED_ASSETS_NAME+'</em>';
	str+='<em style="width: 12%; text-align: center;">'+obj.DEPARTMENT_NAME+'</em>';
	str+='<em style="width: 12%; text-align: center;">'+obj.STORED_IN+'</em>';
	str+='<em style="width: 12%; text-align: center;">'+obj.WITH_MANANGER+'</em>';
	str+='</li>';
	$(result).append(str);
}

function subButton(){
	$('#subImg').unbind('click',subButton);
	return jQuery.formValidator.pageIsValid(1);
}
